package application;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import tools.Tool;

public class CanvasController implements MouseListener, MouseMotionListener, KeyListener
{
	// Components
	protected CanvasView canvas;
	protected String [] shapeType;
	protected int [][] positions;
	protected int counter = 0;
	
	// Constructor
	public CanvasController(CanvasView c)
	{
		canvas = c;
		shapeType = new String [100];
		positions = new int [100][4];
	}
	
	public String [] getShapeType(){ return shapeType; }
	public int [][] getPositions(){ return positions; }
	
	/****< Event Handlers >******************************************************/
	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
	 */
	public void mousePressed(MouseEvent e)
	{
		Tool tool = canvas.getcurrentTool();
		String name = canvas.getCurrentToolName();
		
		if(name == "Rectangle")
		{
			shapeType[counter] = name;
			positions[counter][0] = e.getX();
			positions[counter][1] = e.getY();
		}
		else if(name == "Line")
		{
			shapeType[counter] = name;
			positions[counter][0] = e.getX();
			positions[counter][1] = e.getY();
		}
		else if(name == "Oval")
		{
			shapeType[counter] = name;
			positions[counter][0] = e.getX();
			positions[counter][1] = e.getY();
		}
		else if(name == "FillRect")
		{
			shapeType[counter] = name;
			positions[counter][0] = e.getX();
			positions[counter][1] = e.getY();
		}
		else if(name == "FillOval")
		{
			shapeType[counter] = name;
			positions[counter][0] = e.getX();
			positions[counter][1] = e.getY();
		}
		
		canvas.setShapeColor(counter);
	    if (tool != null) 
	    {
	    	tool.mousePressed(e);
	    }
	}
	  
	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
	 */
	public void mouseReleased(MouseEvent e)
	{ 
	    Tool tool = canvas.getcurrentTool();
	    String name = canvas.getCurrentToolName();
		
		if(name == "Rectangle")
		{
			positions[counter][2] = e.getX();
			positions[counter][3] = e.getY();
			canvas.addToList(name, positions);
			counter++;
		}
		else if(name == "Line")
		{
			positions[counter][2] = e.getX();
			positions[counter][3] = e.getY();
			canvas.addToList(name, positions);
			counter++;
		}
		else if(name == "Oval")
		{
			positions[counter][2] = e.getX();
			positions[counter][3] = e.getY();
			canvas.addToList(name, positions);
			counter++;
		}
		else if(name == "FillRect")
		{
			positions[counter][2] = e.getX();
			positions[counter][3] = e.getY();
			canvas.addToList(name, positions);
			counter++;
		}
		else if(name == "FillOval")
		{
			positions[counter][2] = e.getX();
			positions[counter][3] = e.getY();
			canvas.addToList(name, positions);
			counter++;
		}
		
		
	    if(tool != null)
	    {
	    	tool.mouseReleased(e);
	    }
	}
	  
	/* (non-Javadoc)
	 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
	 */
	public void mouseDragged(MouseEvent e)
	{
	    Tool tool = canvas.getcurrentTool();
	    if(tool != null)
	    {
	    	tool.mouseDragged(e);
	    }
	}
	    
	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	public void mouseClicked(MouseEvent e)
	{
	    Tool tool = canvas.getcurrentTool();
	    if(tool != null)
	    {
	    	tool.mouseClicked(e);
	    }
	}
	  
	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
	 */
	public void mouseEntered(MouseEvent e)
	{
	    Tool tool = canvas.getcurrentTool();
	    if(tool != null)
	    {
	    	tool.mouseEntered(e);
	    }
	}
	  
	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
	 */
	public void mouseExited(MouseEvent e)
	{
	    Tool tool = canvas.getcurrentTool();
	    if(tool != null)
	    {
	    	tool.mouseExited(e);
	    }
	}
	  
	/* (non-Javadoc)
	 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
	 */
	public void mouseMoved(MouseEvent e)
	{
	    Tool tool = canvas.getcurrentTool();
	    if(tool != null)
	    {
	    	tool.mouseMoved(e);
	    }
	}
	    
	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
	 */
	public void keyPressed(KeyEvent e)
	{
	    Tool tool = (Tool)canvas.getcurrentTool();
	    if (tool != null)
	    {
	    	tool.keyPressed(e);
	    }
	}

	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
	 */
	public void keyReleased(KeyEvent e)
	{ 
	    Tool tool = (Tool)canvas.getcurrentTool();
	    if(tool != null)
	    {
	    	tool.keyReleased(e);
	    }
	}

	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
	 */
	public void keyTyped(KeyEvent e)
	{
	    Tool tool = (Tool)canvas.getcurrentTool();
	    if(tool != null)
	    {
	    	tool.keyTyped(e);
	    }// end tool null check
	}// end public void keyTyped( KeyEvent )  
}
